const db = require('../db/sql');

// * 初始化路由模块
// * 初始化路由处理函数模块
// * 获取文章分类列表数据

exports.getArt = (req, res) => {
  const sqlStr = 'select * from article_list where is_delete = 0 order by articleId desc';

  db.query(sqlStr, (err, results) => {
    if (err)
      return res.send({
        success: false,
        message: err,
      });

    res.send({
      success: true,
      message: '获取文章列表成功！',
      data: results,
    });
  });
};

// * 定义路由和处理函数
// * 验证表单数据
// * 查询 分类名称 与 分类别名 是否被占用
// * 实现新增文章分类的功能

exports.addArt = (req, res) => {
  const sqlStr = `select * from article_list where name = ? or alias = ?`;

  db.query(sqlStr, [req.body.name, req.body.alias], (err, results) => {
    if (err)
      return res.send({
        success: false,
        message: err,
      });

    if (results.length === 2)
      res.send({
        success: false,
        message: `《${req.body.name}》文章分类和，《${req.body.name}》文章别名已被占用`,
      });

    if (results.length === 1) {
      if (
        req.body.name === results[0].name &&
        req.body.alias === results[0].alias
      ) {
        return res.send({
          success: false,
          message: `《${req.body.name}》文章分类和，《${req.body.alias}》文章别名已被占用`,
        });
      }

      if (req.body.name === results[0].name) {
        return res.send({
          success: false,
          message: `《${req.body.name}》这个文章分类已被占用`,
        });
      }

      if (req.body.alias === results[0].alias) {
        return res.send({
          success: false,
          message: `《${req.body.alias}》这个文章别名已被占用`,
        });
      }
    }

    const sqlStr = `insert into article_list set ?`;

    db.query(sqlStr, req.body, (err, results) => {
      if (err)
        return res.send({
          success: false,
          message: err,
        });

      if (results.affectedRows !== 1)
        return res.send({
          success: false,
          message: '添加文章列表失败',
        });

      res.send({
        success: true,
        message: '添加文章分类成功',
      });
    });
  });
};

// * 定义路由和处理函数
// * 验证表单数据
// * 实现删除文章分类的功能

exports.delArt = (req, res) => {
  const sqlStr = `delete from article_list where articleId = ?`;

  db.query(sqlStr, req.params.articleId, (err, results) => {
    if (err)
      return res.send({
        success: false,
        message: err,
      });

    if (req.params.articleId < 5)
      return res.send({
        success: false,
        message: '默认项禁止删除',
      });

    if (results.affectedRows !== 1)
      return res.send({
        success: false,
        message: '删除文章失败!',
      });

    res.send({
      success: true,
      message: '删除文章成功！',
    });
  });
};

// * 根据 Id 获取文章分类数据

exports.getArtCateById = (req, res) => {
  const sqlStr = `select * from article_list where articleId=?`;

  db.query(sqlStr, req.params.articleId, (err, results) => {
    if (err)
      return res.send({
        success: false,
        message: err,
      });

    if (results.length !== 1)
      return res.send({
        success: false,
        message: '获取文章分类数据失败！',
      });

    res.send({
      success: true,
      message: '获取文章列表成功！',
      data: results,
    });
  });
};

// * 更新列表分类

exports.updateCateById = (req, res) => {
  const sqlStr = `select * from article_list where articleId<>? and (name=? or alias=?)`;

  db.query(
    sqlStr,
    [req.body.Id, req.body.name, req.body.alias],
    (err, results) => {
      if (err)
        return res.send({
          success: false,
          message: err,
        });

      if (results.length === 2)
        res.send({
          success: false,
          message: `《${req.body.name}》文章分类和，《${req.body.name}》文章别名已被占用`,
        });

      if (results.length === 1) {
        if (
          req.body.name === results[0].name &&
          req.body.alias === results[0].alias
        ) {
          return res.send({
            success: false,
            message: `《${req.body.name}》文章分类和，《${req.body.alias}》文章别名已被占用`,
          });
        }

        if (req.body.name === results[0].name) {
          return res.send({
            success: false,
            message: `《${req.body.name}》这个文章分类已被占用`,
          });
        }

        if (req.body.alias === results[0].alias) {
          return res.send({
            success: false,
            message: `《${req.body.alias}》这个文章别名已被占用`,
          });
        }
      }

      const sqlStr = `update article_list set ? where articleId = ?`;

      db.query(sqlStr, [req.body, req.body.Id], (err, results) => {
        if (err)
          return res.send({
            success: false,
            message: err,
          });

        if (results.affectedRows !== 1)
          return res.send({
            success: false,
            message: '修改失败',
          });

        res.send({
          success: true,
          message: '修改文章列表成功',
        });
      });
    }
  );
};
